<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/14
 * Time: 14:31
 */
namespace app\index\controller;

use app\common\lib\ali\Sms;
use app\common\lib\Redis;
use app\common\lib\Util;

class Send
{

    public function index()
    {
        $phone_num = intval($_POST['phone_num']);
        if(empty($phone_num)){
            return Util::show(config('code.error'), 'phone is error');
        }
        $code = rand(1000, 9999);
        $task_data = [
            'method' => 'sendSms',
            'data' =>[
                'phone' => $phone_num,
                'code' => $code
            ]
        ];
        $_POST['http_server']->task($task_data);
        /*
        try{
            $result = Sms::sendSms($phone_num, $code);
        }catch (\Exception $e){
            //todo
            return Util::show(config('code.error'), '阿里大于内部错误');
        }*/
        return Util::show(config('code.success'), 'send sms success!');
        /*
        if($result->Code === 'OK'){
            //发送成功，将验证码存入redis
            $redis = new \Swoole\Coroutine\Redis();
            $redis->connect(config('redis.host'), config('redis.port'));
            $redis->auth(config('redis.password'));
            $redis->set(Redis::smsKey($phone_num), $code, config('redis.out_time'));

            return Util::show(config('code.success'), 'success');
        }else{
            return Util::show(config('code.error'), '验证码发送失败');
        }*/
    }
}